from flask import Blueprint, jsonify, request, g
import json
from loguru import logger
from app.err_no import errno_no
import traceback
from app.db import dbStatsQuery
from app.db.mongoClient import MongoError

blueprint = Blueprint('stats_query', __name__, url_prefix='/api/stats')


@blueprint.route('/v1/query_report', methods=['POST'])
def query_report():
    '''
    查询餐厅档口消费汇总
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        start_time = j_data.get('start_time')
        end_time = j_data.get('end_time')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or start_time is None or len(start_time) <= 0 \
            or end_time is None or len(end_time) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        # 如果是查询全部档口
        if stallNo == 'all':
            res = dbStatsQuery.queryAllReport(diningNo, start_time, end_time)
            return jsonify(errno_no.OK.data(res))      
        
        res = dbStatsQuery.queryReport(diningNo, stallNo, start_time, end_time)

        return jsonify(errno_no.OK.data(res))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/query_order', methods=['POST'])
def query_order():
    '''
    查询餐厅档口消费明细
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        start_time = j_data.get('start_time')
        end_time = j_data.get('end_time')
        bill_state = j_data.get('bill_state')
        pay_type = j_data.get('pay_type')
        m_from = j_data.get('from')
        m_count = j_data.get('count')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or start_time is None or len(start_time) <= 0 \
            or end_time is None or len(end_time) <= 0 \
            or bill_state is None or len(bill_state) <= 0 \
            or pay_type is None or len(pay_type) <= 0 \
            or m_from is None or len(m_from) <= 0 \
            or m_count is None or len(m_count) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStatsQuery.queryOrder(diningNo, stallNo, start_time, end_time, bill_state, pay_type, m_from, m_count)

        return jsonify(errno_no.OK.data({'orderList': res}))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/query_amountBillCount_someDay', methods=['POST'])
def query_amountBillCount_someDay():
    '''
    查询每天的统计信息
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        start_time = j_data.get('start_time')
        end_time = j_data.get('end_time')
        deviceNo = j_data.get('device_no')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or start_time is None or len(start_time) <= 0 \
            or end_time is None or len(end_time) <= 0 \
            or deviceNo is None or len(deviceNo) <= 0:
            return jsonify(errno_no.ErrServerParam.data(None))

        data = dbStatsQuery.queryAmountBillCount_someDay(diningNo, stallNo, start_time, end_time, deviceNo)
        return jsonify(errno_no.OK.data(data))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/query_mealDayCount', methods=['POST'])
def query_mealDayCount():
    '''
    当餐/当天份数金额
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('device_no')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or deviceNo is None or len(deviceNo) <= 0:
            return jsonify(errno_no.ErrServerParam.data(None))

        data = dbStatsQuery.queryMealDayCount(diningNo, stallNo, deviceNo)
        return jsonify(errno_no.OK.data(data))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    
    
@blueprint.route('/v1/query_note_order', methods=['POST'])
def query_note_order():
    '''
    获取小票打印内容
    '''
    try:
        j_data = json.loads(request.data)
        billId = j_data.get('billId')
        if billId is None or len(billId) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        data = dbStatsQuery.query_note_order(billId)
        if data is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))
        
        return jsonify(errno_no.OK.data(data))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
